package com.hzvdvmc.autosale.setting.ui;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hzvdvmc.autosale.MyApp;
import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.common.OnItemClickListener;
import com.hzvdvmc.autosale.constant.CmdEnum;
import com.hzvdvmc.autosale.sale.MainActivity;
import com.hzvdvmc.autosale.sale.ui.ComonTextDialog;
import com.hzvdvmc.autosale.setting.adapter.ArrowTextRvAdapter;
import com.hzvdvmc.autosale.setting.adapter.SimpleTextRvAdapter;
import com.hzvdvmc.autosale.setting.bean.ArrowItemBean;
import com.hzvdvmc.autosale.setting.bean.RoleEnum;
import com.hzvdvmc.autosale.setting.bean.SettingFuncEnum;
import com.hzvdvmc.autosale.socket.SocketClientManager;
import com.hzvdvmc.autosale.socket.event.CommonEvent;
import com.hzvdvmc.autosale.socket.manager.CmdRequestManager;
import com.hzvdvmc.autosale.socket.manager.SocketLisenter;
import com.hzvdvmc.autosale.socket.manager.SocketRequest;
import com.hzvdvmc.autosale.util.LogUtil;
import com.hzvdvmc.autosale.util.MachineUtil;
import com.hzvdvmc.autosale.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择功能
 */
public class SelectFunctionActivity extends ActionbarActivity {

    @BindView(R.id.rv_func)
    RecyclerView rvFunc;
    @BindView(R.id.tv_account)
    TextView tvAccount;


    private RoleEnum role;
    private String account;

    private List<ArrowItemBean> dataList = new ArrayList<>();

    private ArrowTextRvAdapter mAdapter;

    public static void startMe(Context context, RoleEnum role, String account) {
        Intent intent = new Intent(context, SelectFunctionActivity.class);
        intent.putExtra("role", role);
        intent.putExtra("account", account);
        context.startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventReceived(CommonEvent event) {
        switch (event.getValue()) {
            case CommonEvent.ONE_KEY_SUPP:
                ToastUtils.showShort("一键补货成功");
                break;
            case CommonEvent.ONE_KEY_SUPP_FAIL:
                ToastUtils.showShort("一键补货失败，请重试");
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_function);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        parseIntent();

        initRecyclerView();
        initData();
        setCustomeActiorBar("系统设置");
        tvAccount.setText(role.getRole() + ":" + account);
        LogUtil.p(tvAccount.getText().toString() + "进入了系统设置页");
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }

    private void parseIntent() {
        role = (RoleEnum) getIntent().getSerializableExtra("role");
        account = getIntent().getStringExtra("account");
    }

    private void initData() {
        dataList.add(new ArrowItemBean(SettingFuncEnum.ONEKEY_SUPPLE.getName(), false));//一键补货

        if (role.getPermission() >= RoleEnum.Factory.getPermission()) {
            dataList.add(new ArrowItemBean(SettingFuncEnum.TUNE_SETTING.getName(), true));//货道设置
        }
        dataList.add(new ArrowItemBean(SettingFuncEnum.MACHINE_TEST.getName(), true));//电机测试
        dataList.add(new ArrowItemBean(SettingFuncEnum.MODIFY_PSW.getName(), true));//修改密码
        dataList.add(new ArrowItemBean(SettingFuncEnum.LOOK_LOG.getName(), true));//查看日志
        dataList.add(new ArrowItemBean(SettingFuncEnum.INFRARED_RAY.getName(), true));//红外设置
        dataList.add(new ArrowItemBean(SettingFuncEnum.COLD_SET.getName(), true));//压缩机设置

//        dataList.add(SettingFuncEnum.RECONNECT.getName());//socket重连
        dataList.add(new ArrowItemBean(SettingFuncEnum.ABOUT_MACHINE.getName(), true));//关于设备
        dataList.add(new ArrowItemBean(SettingFuncEnum.TIME_SET.getName(), true));//关于设备
//        dataList.add(SettingFuncEnum.BACK_HOME.getName());//返回桌面
        dataList.add(new ArrowItemBean(SettingFuncEnum.RESTART_APP.getName(), false));//重启应用
        dataList.add(new ArrowItemBean(SettingFuncEnum.EXIT_APP.getName(), false));//退出程序

        mAdapter.notifyDataSetChanged();
    }

    private void initRecyclerView() {
        mAdapter = new ArrowTextRvAdapter(dataList);
        rvFunc.setAdapter(mAdapter);
        rvFunc.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                onItemClick(position);
            }
        });
    }

    private void onItemClick(int position) {
        String data = dataList.get(position).getItemTxt();
        if (SettingFuncEnum.TUNE_SETTING.getName().equals(data)) {
            startActivity(new Intent(this, TuneSetActivity.class));
            LogUtil.p(account + "进入货道设置");
        } else if (SettingFuncEnum.ONEKEY_SUPPLE.getName().equals(data)) {
            showOneKeySuppleDialog();
        } else if (SettingFuncEnum.MODIFY_PSW.getName().equals(data)) {
            ModifyPswActivity.startMe(this, account, role.getPermission());
            LogUtil.p(account + "进入修改密码");
        } else if (SettingFuncEnum.RECONNECT.getName().equals(data)) {
            showReConnectDialog();
            LogUtil.p(account + "点击重连");
        } else if (SettingFuncEnum.ABOUT_MACHINE.getName().equals(data)) {
            AboutActivity.startMe(this, role);
            LogUtil.p(account + "进入关于设备");
        } else if (SettingFuncEnum.MACHINE_TEST.getName().equals(data)) {
            startActivity(new Intent(this, MachineTestActivity.class));
            LogUtil.p(account + "进入电机测试");
        } else if (SettingFuncEnum.BACK_HOME.getName().equals(data)) {
            LogUtil.p(account + "返回到系统桌面");
            backSystemHome();
        } else if (SettingFuncEnum.LOOK_LOG.getName().equals(data)) {
            openFolder();
        } else if (SettingFuncEnum.INFRARED_RAY.getName().equals(data)) {
            LogUtil.p(account + "进入红外设置");
            startActivity(new Intent(this, InfraredRayActivity.class));
        } else if (SettingFuncEnum.EXIT_APP.getName().equals(data)) {
            LogUtil.p(account + "退出程序");
            exitAppConfirm();
        } else if (SettingFuncEnum.RESTART_APP.getName().equals(data)) {
            LogUtil.p(account + "重启应用");
            restartAppConfirm(this);
        } else if (SettingFuncEnum.COLD_SET.getName().equals(data)) {
            LogUtil.p(account + "压缩机设置");
            coldSet();
        } else if (SettingFuncEnum.TIME_SET.getName().equals(data)) {
            startActivity(new Intent(this, TimeSetActivity.class));
        }
    }

    /**
     * 压缩机设置
     */
    private void coldSet() {
        startActivity(new Intent(this, ColdSetActivity.class));
    }

    private void restartAppConfirm(final Context context) {
        final ComonTextDialog dialog = new ComonTextDialog(this);
        dialog.show();

        dialog.setContent("确认重启程序吗?");
        dialog.setTitle("确认");
        dialog.setOkOnClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.p(account + "重启程序");
                restartApp();

            }
        });
    }

    //退出app
    private void exitAppConfirm() {

        final ComonTextDialog dialog = new ComonTextDialog(this);
        dialog.show();

        dialog.setContent("确认退出程序吗?");
        dialog.setTitle("确认");
        dialog.setOkOnClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                LogUtil.p(account + "退出程序");

                exitApp();

            }
        });
    }


    private void restartApp() {
        ((MyApp) getApplication()).restartApp();
    }


    private void exitApp() {
        ((MyApp) getApplication()).exitApp();
    }

    private void showReConnectDialog() {
        final ComonTextDialog dialog = new ComonTextDialog(this);
        dialog.show();

        dialog.setContent("确认重连服务器吗?");
        dialog.setTitle("确认");
        dialog.setOkOnClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                SocketClientManager.getInstance().connect();
                LogUtil.p("发起 重连服务器");

            }
        });

    }

    private void showOneKeySuppleDialog() {
        final ComonTextDialog dialog = new ComonTextDialog(this);
        dialog.show();

        dialog.setContent("确认一键补货吗?");
        dialog.setTitle("确认");
        dialog.setOkOnClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                supplementByOne();
                LogUtil.p("request 一键补货");
            }
        });
    }


    /**
     * 一键补货（上报）
     */
    private void supplementByOne() {
        SocketRequest request = new SocketRequest(CmdEnum.REPORT_SUPPLEMENT_BY_ONE);
        request.equence(MachineUtil.getDeviceNO(), new SocketLisenter() {
            @Override
            public void onResponse(String response, String tuneNO, String orderNO) {

            }

            @Override
            public void onError(String data) {

            }
        });
    }

    private void backSystemHome() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    private void openFolder() {
        Intent intent = new Intent(this, LogFileActivity.class);
        startActivity(intent);
    }

}
